package com.gosophia.conference.action.scheme;

import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.List;

import net.sf.json.JSONArray;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;

import com.gosophia.commons.web.action.BaseSupportAction;
import com.gosophia.conferenceManagement.scheme.dto.SchemeJobDataDTO;
import com.gosophia.conferenceManagement.scheme.dto.SchemeTransientStoreDTO;
import com.gosophia.conferenceManagement.scheme.ws.ConferenceSchemeWebService;
import com.gosophia.dataDictionary.dto.DataValueDTO;
import com.gosophia.dataDictionary.ws.DataDictionaryWebService;
import com.gosophia.oa.action.JudgeCurrentUndertaker;
import com.gosophia.springsecurity.UserDetailsImpl;
import com.gosophia.taskManagement.dto.JobDTO;
import com.gosophia.taskManagement.ws.TaskManagementWebService;
import com.gosophia.userManagement.dto.UserInfoDTO;
import com.gosophia.userManagement.ws.UserManagementWebService;

@Scope("prototype")
@Controller("ToEditSchemeTransientStoreAction")
public class ToEditSchemeTransientStoreAction extends BaseSupportAction {
	private static final long serialVersionUID = 1L;
	private Long schemeTransientStoreId;
	private SchemeTransientStoreDTO schemeTransientStoreDTO;
	private static final Long IMPORTANCE_ENTRYID = 1014L;
	@Autowired
	private DataDictionaryWebService dataDictionaryWebService;
	@Autowired
	private ConferenceSchemeWebService conferenceSchemeWebService;
	@Autowired
	private UserManagementWebService userManagementWebService;
	@Autowired
	private TaskManagementWebService taskManagementWebService;
	@Autowired
    private JudgeCurrentUndertaker judgeCurrentUndertaker;
	private List<DataValueDTO> importanceList;
	private String onlinePath;
	private String onlineVersion;
	private String userInfoString;
	private String dateTime;
	private String planEndTime;
	private Long jobId;
	//会议上一Job的业务数据
	private SchemeJobDataDTO preSchemeJobDataDTO;
	@Override
	public String _execute() throws Exception {
		// 获取重要程度列表
		 importanceList = dataDictionaryWebService
				.findEnabledDataValueByEntryId(IMPORTANCE_ENTRYID);
		if (importanceList != null) {
			Collections.reverse(importanceList);
		}
		//获取联系人
		List<UserInfoDTO> userInfoDTOs = userManagementWebService.findAllUserInfoHasAccount();
		if(userInfoDTOs!=null && userInfoDTOs.size()!=0){
		    userInfoString=JSONArray.fromObject(userInfoDTOs).toString().replace("\"", "\\\"");
		}
		// 得到当前用户
		UserDetailsImpl userDetails = (UserDetailsImpl) SecurityContextHolder
				.getContext().getAuthentication().getPrincipal();
		UserInfoDTO userInfoDTO = userDetails.getUserAccountDTO().getUserInfoDTO();
		//根据jobId查找任务信息
		if(jobId!=null){
			 judgeCurrentUndertaker.judgeOperationAuthority(jobId);
			 JobDTO jobDto = taskManagementWebService.findJobDetailByJobId(jobId);
			 schemeTransientStoreDTO = new SchemeTransientStoreDTO();
			 schemeTransientStoreDTO.setCreatorDTO(userInfoDTO);
			 schemeTransientStoreDTO.setConferenceName(jobDto.getMissionDTO().getMissionName());
			 SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd"); //格式化当前系统日期
			 planEndTime = dateFm.format(jobDto.getMissionDTO().getScheduleEndDate());
			 schemeTransientStoreDTO.setPlanEndDate(jobDto.getMissionDTO().getScheduleEndDate());
			 schemeTransientStoreDTO.setDescription(jobDto.getMissionDTO().getDescription());
			 schemeTransientStoreDTO.setImportanceDTO(jobDto.getMissionDTO().getImportanceDTO());
			 schemeTransientStoreDTO.setJobId(jobId);
			 if(schemeTransientStoreId !=null){
					schemeTransientStoreDTO = conferenceSchemeWebService.findTransientStoreById(schemeTransientStoreId);
					if(schemeTransientStoreDTO.getSchemeDocumentDTO()!=null){
					    onlinePath=schemeTransientStoreDTO.getSchemeDocumentDTO().getDocumentPath();
			            onlineVersion = schemeTransientStoreDTO.getSchemeDocumentDTO().getDocumentVersion();
					}
					
				}
		}else{
			if(schemeTransientStoreId !=null){
				schemeTransientStoreDTO = conferenceSchemeWebService.findTransientStoreById(schemeTransientStoreId);
				SimpleDateFormat dateFm = new SimpleDateFormat("yyyy-MM-dd"); //格式化当前系统日期
				dateTime = dateFm.format(schemeTransientStoreDTO.getPlanEndDate());
				if(schemeTransientStoreDTO.getSchemeDocumentDTO()!=null){
				    onlinePath=schemeTransientStoreDTO.getSchemeDocumentDTO().getDocumentPath();
		            onlineVersion = schemeTransientStoreDTO.getSchemeDocumentDTO().getDocumentVersion();
				}
				
			}
		}
		
		return SUCCESS;
	}

	public Long getSchemeTransientStoreId() {
		return schemeTransientStoreId;
	}

	public void setSchemeTransientStoreId(Long schemeTransientStoreId) {
		this.schemeTransientStoreId = schemeTransientStoreId;
	}
	public SchemeTransientStoreDTO getSchemeTransientStoreDTO() {
		return schemeTransientStoreDTO;
	}

	public void setSchemeTransientStoreDTO(
			SchemeTransientStoreDTO schemeTransientStoreDTO) {
		this.schemeTransientStoreDTO = schemeTransientStoreDTO;
	}

	public List<DataValueDTO> getImportanceList() {
		return importanceList;
	}

	public void setImportanceList(List<DataValueDTO> importanceList) {
		this.importanceList = importanceList;
	}

	public String getUserInfoString() {
		return userInfoString;
	}

	public void setUserInfoString(String userInfoString) {
		this.userInfoString = userInfoString;
	}

	public String getOnlinePath() {
		return onlinePath;
	}

	public void setOnlinePath(String onlinePath) {
		this.onlinePath = onlinePath;
	}

	public String getOnlineVersion() {
		return onlineVersion;
	}

	public void setOnlineVersion(String onlineVersion) {
		this.onlineVersion = onlineVersion;
	}

	public String getDateTime() {
		return dateTime;
	}

	public void setDateTime(String dateTime) {
		this.dateTime = dateTime;
	}

	public Long getJobId() {
		return jobId;
	}

	public void setJobId(Long jobId) {
		this.jobId = jobId;
	}

	public SchemeJobDataDTO getPreSchemeJobDataDTO() {
		return preSchemeJobDataDTO;
	}

	public void setPreSchemeJobDataDTO(SchemeJobDataDTO preSchemeJobDataDTO) {
		this.preSchemeJobDataDTO = preSchemeJobDataDTO;
	}

	public String getPlanEndTime() {
		return planEndTime;
	}

	public void setPlanEndTime(String planEndTime) {
		this.planEndTime = planEndTime;
	}
	

}
